package com.yjymm.commonsdk.common.exception.advice;

import com.yjymm.commonsdk.common.exception.BusinessException;
import com.yjymm.commonsdk.model.CommonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author panhaoxiong
 * @date 2021-02-07 16:25
 */

@ControllerAdvice
@RestController
public class CommonBaseAdvice {

    @ExceptionHandler({BusinessException.class})
    public CommonResult defaultHandle(BusinessException e) {
        return build(e);
    }

    public CommonResult build(BusinessException e) {
        return CommonResult.builder()
                .success(false)
                .code(e.getCode())
                .message(e.getMessage())
                .detailMessage(e.getCause().getMessage())
                .build();
    }
}
